WOLF core
WOLF: A Modular Estimation Framework for Robotics
XXXXXXXXXXXXXXXXXXXXXXXXvoid(0)" frameborder="0" name="MSearchResults" id="MSearchResults">
state_block.h
Go to the documentation of this file.
1// WOLF - Copyright (C) 2020-2026
2// Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
3// Authors: Joan Solà Ortega (jsola@iri.upc.edu) and
4// Joan Vallvé Navarro (jvallve@iri.upc.edu)
5// All rights reserved.
6//
7// This file is part of WOLF: http://www.iri.upc.edu/wolf
8// WOLF is free software: you can redistribute it and/or modify
9// it under the terms of the GNU General Public License version 3
10// as published by the Free Software Foundation.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program. If not, see <http://www.gnu.org/licenses/>.
19#pragma once
20
21/*
22 * Macro for defining StateBlocks creators.
23 *
24 * Place a call to this macro inside your class declaration
25 * (in the state_block_class.h file), preferably just after the constructors.
26 *
27 * In order to use this macro, the derived state_block class, StateBlockClass,
28 * must have the constructor callable as:
29 *
30 * StateBlockClass(const Eigen::VectorXd& _state, bool _fixed)
31 *
32 * And the static method:
33 *
34 * static Eigen::VectorXd Identity()
35 */
36#define WOLF_STATE_BLOCK_CREATE(StateBlockClass) \
37 static StateBlockPtr create(const Eigen::VectorXd& _state, bool _fixed) \
38 { \
39 return std::make_shared<StateBlockClass>(_state, _fixed); \
40 } \
41 static StateBlockPtr createIdentity(bool _fixed) \
42 { \
43 return std::make_shared<StateBlockClass>(StateBlockClass::Identity(), _fixed); \
44 }
45
46// Fwd references
47namespace wolf
48{
49class NodeBase;
50class LocalParametrizationBase;
51} // namespace wolf
52
53// Wolf includes
54#include "core/common/wolf.h"
56
57// std includes
58#include <iostream>
59#include <mutex>
60
61namespace wolf
62{
72class StateBlock : public std::enable_shared_from_this<StateBlock>
73{
74 public:
75 protected:
76 std::string type_;
77 std::atomic_bool fixed_;
78
79 std::atomic<SizeEigen> state_size_;
80 Eigen::VectorXd state_;
81 mutable std::mutex mut_state_;
82
83 LocalParametrizationBasePtr local_param_ptr_;
84
85 std::atomic_bool state_updated_;
86 std::atomic_bool fix_updated_;
87 std::atomic_bool local_param_updated_;
88
90
91 public:
98 StateBlock(const std::string& _type,
99 const SizeEigen _size,
100 bool _fixed = false,
101 LocalParametrizationBasePtr _local_param_ptr = nullptr,
102 bool _transformable = true);
103
110 StateBlock(const std::string& _type,
111 const Eigen::VectorXd& _state,
112 bool _fixed = false,
113 LocalParametrizationBasePtr _local_param_ptr = nullptr,
114 bool _transformable = true);
115
118 StateBlock(const StateBlock& o) = delete;
119
122 StateBlock(StateBlock&& o) = delete;
123
126 StateBlock& operator=(const StateBlock& o) = delete;
127
130 virtual ~StateBlock();
131
134 std::string getType() const;
135
138 Eigen::VectorXd getState() const;
139
142 double* getStateData();
143
146 void setState(const Eigen::VectorXd& _state, const bool _notify = true);
147
150 SizeEigen getSize() const;
151
154 SizeEigen getLocalSize() const;
155
158 bool isFixed() const;
159
162 void fix();
163
166 void unfix();
167
170 void setFixed(bool _fixed);
171
174 bool hasLocalParametrization() const;
175
178 LocalParametrizationBasePtr getLocalParametrization() const;
179
182 void setLocalParametrization(LocalParametrizationBasePtr _local_param);
183
187
190 bool stateUpdated() const;
191
194 bool fixUpdated() const;
195
198 bool localParamUpdated() const;
199
202 void resetStateUpdated();
203
206 void resetFixUpdated();
207
211
214 virtual void setIdentity(bool _notify = true);
215
218 void setZero(bool _notify = true);
219
222 virtual Eigen::VectorXd identity() const;
223
226 Eigen::VectorXd zero() const;
227
231 void perturb(double amplitude = 0.1);
232
235 bool isTransformable() const
236 {
237 return transformable_;
238 }
239
242 void setTransformable(bool _trf = true)
243 {
244 transformable_ = _trf;
245 }
246
250 {
251 transformable_ = false;
252 }
253
256 virtual void transform(const VectorComposite& _transformation) = 0;
257
260 void plus(const Eigen::VectorXd& _dv);
261
264 bool isValid(double tolerance = Constants::EPS);
265};
266
267} // namespace wolf
268
269// IMPLEMENTATION
272#include "core/problem/problem.h"
273
274namespace wolf
275{
276inline StateBlock::StateBlock(const std::string& _type,
277 const Eigen::VectorXd& _state,
278 bool _fixed,
279 LocalParametrizationBasePtr _local_param_ptr,
280 bool _transformable)
281 : type_(_type),
282 fixed_(_fixed),
283 state_size_(_state.size()),
284 state_(_state),
285 local_param_ptr_(_local_param_ptr),
286 state_updated_(false),
287 fix_updated_(false),
288 local_param_updated_(false),
289 transformable_(_transformable)
290{
291 // std::cout << "constructed +sb" << std::endl;
292}
293
294inline StateBlock::StateBlock(const std::string& _type,
295 const SizeEigen _size,
296 bool _fixed,
297 LocalParametrizationBasePtr _local_param_ptr,
298 bool _transformable)
299 : type_(_type),
300 fixed_(_fixed),
301 state_size_(_size),
302 state_(Eigen::VectorXd::Zero(_size)),
303 local_param_ptr_(_local_param_ptr),
304 state_updated_(false),
305 fix_updated_(false),
306 local_param_updated_(false),
307 transformable_(_transformable)
308{
309 // std::cout << "constructed +sb" << std::endl;
310}
311
313{
314 // std::cout << "destructed -sb" << std::endl;
315}
316
317inline std::string StateBlock::getType() const
318{
319 return type_;
320}
321
322inline Eigen::VectorXd StateBlock::getState() const
323{
324 std::lock_guard<std::mutex> lock(mut_state_);
325 return state_;
326}
327
329{
330 return state_size_.load();
331}
332
334{
335 if (local_param_ptr_) return local_param_ptr_->getLocalSize();
336 return getSize();
337}
338
339inline bool StateBlock::isFixed() const
340{
341 return fixed_.load();
342}
343
344inline void StateBlock::fix()
345{
346 setFixed(true);
347}
348
349inline void StateBlock::unfix()
350{
351 setFixed(false);
352}
353
355{
356 return (local_param_ptr_ != nullptr);
357}
358
359inline LocalParametrizationBasePtr StateBlock::getLocalParametrization() const
360{
361 return local_param_ptr_;
362}
363
365{
366 assert(local_param_ptr_ != nullptr && "state block without local parametrization");
367 local_param_ptr_.reset();
368 local_param_updated_.store(true);
369}
370
371inline void StateBlock::setLocalParametrization(LocalParametrizationBasePtr _local_param)
372{
373 assert(_local_param != nullptr && "setting a null local parametrization");
374 local_param_ptr_ = _local_param;
375 local_param_updated_.store(true);
376}
377
378inline bool StateBlock::stateUpdated() const
379{
380 return state_updated_.load();
381}
382
383inline bool StateBlock::fixUpdated() const
384{
385 return fix_updated_.load();
386}
387
389{
390 return local_param_updated_.load();
391}
392
394{
395 state_updated_.store(false);
396}
397
399{
400 fix_updated_.store(false);
401}
402
404{
405 local_param_updated_.store(false);
406}
407
409{
410 return state_.data();
411}
412
413inline void StateBlock::setIdentity(bool _notify)
414{
415 setState(Eigen::VectorXd::Zero(state_size_), _notify);
416}
417
418inline void StateBlock::setZero(bool _notify)
419{
420 setIdentity(_notify);
421}
422
423inline Eigen::VectorXd StateBlock::identity() const
424{
425 return Eigen::VectorXd::Zero(state_size_);
426}
427
428inline Eigen::VectorXd StateBlock::zero() const
429{
430 return identity();
431}
432
433inline bool StateBlock::isValid(double tolerance)
434{
435 return local_param_ptr_ ? local_param_ptr_->isValid(state_, tolerance) : true;
436}
437
438} // namespace wolf
State block: a state vector of a variable of the nonlinear optimization.
Definition: state_block.h:73
StateBlock & operator=(const StateBlock &o)=delete
Not assignable.
StateBlock(StateBlock &&o)=delete
Not movable.
void perturb(double amplitude=0.1)
Perturbs the state (if not fixed) with random noise following a uniform distribution in [-amplitude,...
Definition: state_block.cpp:47
std::string getType() const
Returns the type of the state block.
Definition: state_block.h:317
bool localParamUpdated() const
Returns whether the local parametrization has been updated (to be notified to the solver)
Definition: state_block.h:388
Eigen::VectorXd zero() const
Returns the zero (identity) element of the state.
Definition: state_block.h:428
void resetStateUpdated()
Resets the state updated flag.
Definition: state_block.h:393
std::atomic_bool local_param_updated_
Flag to indicate whether the local parametrization has been updated.
Definition: state_block.h:87
std::string type_
Type of the derived class.
Definition: state_block.h:76
bool isFixed() const
Returns whether the state is fixed (not estimated)
Definition: state_block.h:339
bool isTransformable() const
Returns whether the state block can be transformed to another reference frame.
Definition: state_block.h:235
SizeEigen getSize() const
Returns the state size.
Definition: state_block.h:328
void unfix()
Sets the state as estimated.
Definition: state_block.h:349
virtual void setIdentity(bool _notify=true)
Sets the state to the identity element.
Definition: state_block.h:413
void resetFixUpdated()
Resets the fixed status updated flag.
Definition: state_block.h:398
bool transformable_
Flag to indicate if the state block can be transformed to another reference frame.
Definition: state_block.h:89
virtual void transform(const VectorComposite &_transformation)=0
Transforms the state to another reference frame (if transformable)
void setNonTransformable()
Sets the state block as not transformable to another reference frame.
Definition: state_block.h:249
bool isValid(double tolerance=Constants::EPS)
Returns whether the state is valid according to its local parametrization (e.g. unit quaternion)
Definition: state_block.h:433
bool stateUpdated() const
Returns whether the state has been updated (to be notified to the solver)
Definition: state_block.h:378
LocalParametrizationBasePtr getLocalParametrization() const
Returns the state local parametrization ptr.
Definition: state_block.h:359
std::atomic_bool state_updated_
Flag to indicate whether the state has been updated or not.
Definition: state_block.h:85
void plus(const Eigen::VectorXd &_dv)
Adds a (tangent) increment to the state, through the local parametrization if any.
Definition: state_block.cpp:56
void setState(const Eigen::VectorXd &_state, const bool _notify=true)
Sets the state vector.
Definition: state_block.cpp:25
std::mutex mut_state_
State vector mutex.
Definition: state_block.h:81
Eigen::VectorXd getState() const
Returns the state vector.
Definition: state_block.h:322
StateBlock(const std::string &_type, const SizeEigen _size, bool _fixed=false, LocalParametrizationBasePtr _local_param_ptr=nullptr, bool _transformable=true)
Constructor from size.
Definition: state_block.h:294
void setZero(bool _notify=true)
Sets the state to zero (the identity element)
Definition: state_block.h:418
virtual Eigen::VectorXd identity() const
Returns the identity element of the state.
Definition: state_block.h:423
void fix()
Sets the state as fixed.
Definition: state_block.h:344
SizeEigen getLocalSize() const
Returns the size of the local parametrization.
Definition: state_block.h:333
virtual ~StateBlock()
Destructor.
Definition: state_block.h:312
void setLocalParametrization(LocalParametrizationBasePtr _local_param)
Sets a local parametrization.
Definition: state_block.h:371
StateBlock(const StateBlock &o)=delete
Not copyable.
void setTransformable(bool _trf=true)
Sets whether the state block can be transformed to another reference frame.
Definition: state_block.h:242
Eigen::VectorXd state_
State vector storing the state values.
Definition: state_block.h:80
LocalParametrizationBasePtr local_param_ptr_
Local parametrization (optimization in the tangent space)
Definition: state_block.h:83
void removeLocalParametrization()
Removes the state_block local parametrization.
Definition: state_block.h:364
bool fixUpdated() const
Returns whether the fixed status has been updated (to be notified to the solver)
Definition: state_block.h:383
void setFixed(bool _fixed)
Sets the state status.
Definition: state_block.cpp:38
std::atomic_bool fix_updated_
Flag to indicate whether the status has been updated or not.
Definition: state_block.h:86
void resetLocalParamUpdated()
Resets the local parametrization updated flag.
Definition: state_block.h:403
std::atomic< SizeEigen > state_size_
State vector size.
Definition: state_block.h:79
double * getStateData()
Returns the state vector data array.
Definition: state_block.h:408
std::atomic_bool fixed_
Flag to indicate whether the state is fixed or not.
Definition: state_block.h:77
bool hasLocalParametrization() const
Returns whether the state has a local parametrization.
Definition: state_block.h:354
Composite of state vectors.
Definition: vector_composite.h:29
Namespace extending Eigen definitions.
Definition: wolf.h:91
const double EPS
Definition: wolf.h:74
Definition: capture_base.h:23
EIGEN_DEFAULT_DENSE_INDEX_TYPE SizeEigen
Vector and Matrices size type for the Wolf project.
Definition: wolf.h:58